package com.s666.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.s666.demo.utils.RSAUtils;
import com.s666.demo.utils.Base64Util;

@Controller
@RequestMapping("/rsa")
public class RsaController {
    private static final String MENU = "menu_privacy";

    private String pubKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDGkO7LU/9l2+J2rBEcg9/rKW5JLBP+aekAccXZcQc3IUE0uSu+eycgCLjli9jw/neikiQA/K2wjS78ckb64gwbLs7q1n6CaH54ZzzwSEa3M1UBYLthK+9/I9lMVDcn7OXwgC8eQJaPYuVv29MDKNPFZIU+qMBlSP7nYDo6llX97QIDAQAB";
    private String priKey="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMaQ7stT/2Xb4nasERyD3+spbkksE/5p6QBxxdlxBzchQTS5K757JyAIuOWL2PD+d6KSJAD8rbCNLvxyRvriDBsuzurWfoJofnhnPPBIRrczVQFgu2Er738j2UxUNyfs5fCALx5Alo9i5W/b0wMo08VkhT6owGVI/udgOjqWVf3tAgMBAAECgYEAvVlxzUThVCGVV/MHMjjzK03L1E0LF92pCfp0X8Cvz7uPcZZuh2rqyVpn7sUn78/JB6byXFqXyrcnKiZTV2R9gzE+viR+YtvjaaCLZ8A+RfraRfv7cQiTkH90kjeNe7UBVWutcLGCca8sQ2FF8JHmB3QAH+AF8XKxIROfXMP2FiECQQD+cpHCnkmoUVXbM+Ft2AbYuTxK/V/EtPPHqhYfPwIH2mgTc74hNN/eexwrY2KdZWlhliopzy2QkkOLl+FDfsBZAkEAx8cUjaUuC716rwvrjn+nq1FACQA6BclxyFTPJqdlY89Rufb0feVFam2vArJ61q+19mzqaNsIu6Eiplstf/AXtQJABOpNqobIQXogwHVGG4saxsD9Sy2N1gzw0KCWgNAEyAYOEKIhwB53XKLgLm5jD0tQN9RgyCH6bVqF94JdlaIzMQJAYnnWvs+qd+0JuZmlnpN8Ye1mJEmxUELxTG/JwOMJDn8WaRW7hQdsBT9V4n16IbNxfaxIjDFUB7ExNFGDhyohdQJBANCkqnofGUxeLKg//LaT90mSbD7FusMJhQ/xTmh2p/21AsgxkxuH4dv3r7VFamf+KHcGEuFZKIv8UmR/wwH36c8=";

    @RequestMapping("/index")
    public String index(Model model) {
        model.addAttribute("menu", MENU);
        return "rsa/index";
    }

    @PostMapping("/enstr")
    public String encrypt(@RequestParam(value = "content", required = true) String content,
                          Model model) {
        String enStr;
        try{
            enStr = encryptRsa(content);
        }catch(Exception e){
            enStr = "加密失败";
        }
        model.addAttribute("content",enStr);
        return "/default/msg";
    }
    @PostMapping("/destr")
    public String decrypt(@RequestParam(value = "content", required = true) String content,
                          Model model) {
        String deStr;
        try{
            content = content.trim();
            deStr = decryptRsa(content);
        }catch(Exception e){
            deStr = "解密失败" + e.toString();
        }
        model.addAttribute("content",deStr);
        return "/default/msg";
    }

    /**
     * 加密
     * @param content
     * @return
     */
    private String encryptRsa(String content) throws Exception{
        byte[] strBytes = content.getBytes();
        byte[] enBytes =  RSAUtils.encryptByPublicKey(strBytes, pubKey);
        String enStr = Base64Util.encode(enBytes);
        return enStr;
    }
    /**
     * 解密
     * @param enStr
     * @return
     */
    private String decryptRsa(String enStr) throws Exception{
        byte[] deBytes = RSAUtils.decryptByPrivateKey(Base64Util.decode(enStr), priKey);
        String deStr = new String(deBytes);
        return deStr;
    }
}

